package dao.impl;

import dao.BaseDao;
import dao.UserDao;
import entity.User;
import utils.Md5Util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    BaseDao bd=new BaseDao();
    @Override
    public int init(User user) {
        int i;
        String sql="INSERT INTO `user` (id,username,`password`,email,createdate,birthday) VALUES (0,?,?,?,sysdate(),?)";
        i=bd.exeUpdate(sql,user.getUsername(),user.getPassword(),user.getEmail(),user.getBirthday());
        return i;
    }

    @Override
    public User selectEmail(String email) {
        String sql="SELECT * FROM `user` WHERE email=?";
        ResultSet rs=bd.query(sql,email);
        User user=null;
        try {
            if(rs.next()){
                user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString(3));
                user.setEmail(rs.getString(4));
                user.setCreatedate(rs.getDate(5));
                user.setBirthday(rs.getDate(6));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public User selectName(String username) {
        String sql="SELECT * FROM `user` WHERE username=?  ";
        ResultSet rs=bd.query(sql,username);
        User user=null;
        try {
            if(rs.next()){
                user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString(3));
                user.setEmail(rs.getString(4));
                user.setCreatedate(rs.getDate(5));
                user.setBirthday(rs.getDate(6));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public User selectID(int id) {
        String sql="SELECT * FROM `user` WHERE id=?";
        ResultSet rs=bd.query(sql,id);
        User user=null;
        try {
            if(rs.next()){
                user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString(3));
                user.setEmail(rs.getString(4));
                user.setCreatedate(rs.getDate(5));
                user.setBirthday(rs.getDate(6));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public int update(User user) {
        String sql="UPDATE `user` SET username=?,email=?,birthday=?  WHERE id=?";
        int i=bd.exeUpdate(sql,user.getUsername(),user.getEmail(),user.getBirthday(),user.getId());
        System.out.println(i);
        return i;
    }

    @Override
    public int update1(String p, int id) {
        String sql="UPDATE `user` SET password=?  WHERE id=?";
        p= Md5Util.stringMd5(p);
        return bd.exeUpdate(sql,p,id);
    }


    @Override
    public List<User> selectAll() {
        String sql="SELECT * FROM `user` ";
        List<User> list=new ArrayList<>();
        ResultSet rs=bd.query(sql);
        try {
            while (rs.next()){
                User user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString(3));
                user.setEmail(rs.getString(4));
                user.setCreatedate(rs.getDate(5));
                user.setBirthday(rs.getDate(6));
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public User selectName2(String username, int id) {
        String sql="SELECT * FROM `user` WHERE username=? and id!=?";
        ResultSet rs=bd.query(sql,username,id);
        User user=null;
        try {
            if(rs.next()){
                user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString(3));
                user.setEmail(rs.getString(4));
                user.setCreatedate(rs.getDate(5));
                user.setBirthday(rs.getDate(6));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public User selectEmail2(String email, int id) {
        String sql="SELECT * FROM `user` WHERE email=? and id!=?";
        ResultSet rs=bd.query(sql,email,id);
        User user=null;
        try {
            if(rs.next()){
                user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString(3));
                user.setEmail(rs.getString(4));
                user.setCreatedate(rs.getDate(5));
                user.setBirthday(rs.getDate(6));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
}
